import api from '@/service/config/api.js'
export default {
	data() {
		return {
			sharePath: '',
			shareTitle: '',
			shareImageUrl: ''
		}
	},
	onLoad: function() {
		uni.showShareMenu({
			withShareTicket: true,
			menus: ["shareAppMessage", "shareTimeline"]
		})
	},
	onShareAppMessage(res) {
		 console.log('sahre')
		let imageUrl = this.shareImageUrl || '';
		let title = this.shareTitle || '您的好友分享了一个家装电商平台，快注册体验吧~';
		let path = this.sharePath || '/pages/index/index';
		if (this.dataParams && Object.keys(this.dataParams).length) {
			path += uni.$u.queryParams(this.dataParams)
		}
		if (res.from === 'button') {
			console.log('11,button')
			//这块需要传参，不然链接地址进去获取不到数据
			// path = `/` + this.$scope.route + `?id=` + this.$scope.options.id;
			if (res.target.dataset.url) {
				path = url
			}
			if (res.target.dataset.imageUrl) {
				imageUrl = imageUrl
			}
			if (res.target.dataset.title) {
				title = title
			}
			return {
				title: title || '您的好友分享了一个家装电商平台，快注册体验吧~',
				path: path,
				imageUrl: imageUrl
			};
		}
		if (res.from === 'menu'&&this.$storage.getItem('token')) {
			api.post('/goods/api/product/addShareStatistics', {
				productId: this.dataParams.goodsId ? this.dataParams.goodsId : this.dataParams.id,
				...this.$store.state.userInfo.customerId ? {
					customerId: this.$store.state.userInfo.customerId
				} : {},
				userType: this.$store.state.userInfo.identityType || 1
			}, {
				noHideLoading: false,
				noToast: false
			})
			return {
				title,
				path,
				imageUrl
			};
		}
	},
	// 分享到朋友圈
	onShareTimeline() {
		 console.log('onShareTimeline')
		 try{
		 	let imageUrl = this.shareImageUrl || '';
		 	let title = this.shareTitle || '您的好友分享了一个家装电商平台，快注册体验吧~';
		 	let query = this.dataParams;
		 	// if (this.dataParams&&Object.keys(this.dataParams).length) {
		 	// 	query = uni.$u.queryParams(this.dataParams)
		 	// }
		 	console.log({
		 		title,
		 		query,
		 		imageUrl
		 	},11)
			if(this.$storage.getItem('token')) {
				api.post('/goods/api/product/addShareStatistics', {
					productId: this.dataParams.goodsId ? this.dataParams.goodsId : this.dataParams.id,
					...this.$store.state.userInfo.customerId ? {
						customerId: this.$store.state.userInfo.customerId
					} : {},
					userType: this.$store.state.userInfo.identityType || 1
				}, {
					noHideLoading: false,
					noToast: false
				})
			}
		 	return {
		 		title,
		 		query,
		 		imageUrl
		 	};
		 }catch(e){
		 	//TODO handle the exception
			console.log(e)
		 }
	},
}